<?php

class ApiController extends Zend_Controller_Action
{
    public function init()
    {
        $this->getHelper('layout')->setLayout('default');
        /*
         * If using http login/password headers set auth code here
         * or within soapAction() if no problem to make the wsdl file public
         * 
         * header('WWW-Authenticate: Basic realm="Emms Api"');
         * header('HTTP/1.0 401 Unauthorized');
         * header('HTTP/1.1 401 Unauthorized', true, 401);
         * throw new SOAPFault("Incorrect username and or password.", 401);
         * exit;
         *
        */
    }
    
    public function soapAction()
    {
        $this->getHelper('viewRenderer')->setNoRender(true);
        $server = new Zend_Soap_Server($this->_getBaseUrl() . '/wsdl');
        $server->setClass('Emms_Api');
        $server->handle();
    }
    
    /**
    * function to generate WSDL.
    */
    public function wsdlAction() 
    {
        $this->getHelper('viewRenderer')->setNoRender(true);
        $wsdl = new Zend_Soap_AutoDiscover();
        $wsdl->setClass('Emms_Api');
        $wsdl->setUri($this->_getBaseUrl() . '/api');
        $wsdl->handle();
    }
    
    private function _getBaseUrl()
    {
        return sprintf("%s://%s",
            isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 
                'https' : 'http', $_SERVER['HTTP_HOST']);
    }
}